home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / ctimer / timer.c
C/C++ Source or Header  |  1987-12-11  |  2KB  |  75 lines

  1. /*============Demonstration of interrupt handling in MSC ver 5.0=============
  2.  
  3.     TIMER.C was created to test the use of interrupt handling in  Microsoft 
  4.     C  5.0.  Timer  is a functional program which can produce a timed delay 
  5.     from a DOS command. To use TIMER enter:
  6.     
  7.             TIMER delay    Where: delay = Seconds to wait
  8. */
  9.  
  10. #include    <stdio.h>
  11. #include    <dos.h>
  12. #include    <stdlib.h>
  13.  
  14. #define        TIMER    8        /* Timer interrupt number */
  15.  
  16. void (_CDECL interrupt far *original)(); /* Pointer to original handler */
  17. volatile unsigned ticks;
  18.  
  19. /*==========================Timer interrupt handler==========================*/
  20.  
  21. void _CDECL interrupt far tick(void);
  22.  
  23. void _CDECL interrupt far tick()
  24.     int a;        /* The "int a;" declaration is a work around for an 
  25.                optimization bug in MSC 5.0 where it unwittingly 
  26.                removes code to save the stack pointer, not 
  27.                knowing that _chain_intr() is about to be used. 
  28.                Any automatic variable will prevent problems.   */
  29.                    
  30.     ticks++;            /* Count one tick */
  31.     _chain_intr( original );    /* This calls the original handler */
  32. }
  33.  
  34. /*===================================Main====================================*/
  35.  
  36. main( argc, argv )
  37.   int argc;
  38.   char **argv;
  39. {
  40.     unsigned delay;
  41.  
  42.         /* Test usage and use bdos() to avoid bringing in
  43.            a whole lot of code with the prinf() function  */
  44.            
  45.     if( argc!=2 || (delay=atoi(argv[1]))==0 )    
  46.         {
  47.         bdos( 9, (unsigned)"Usage: TIMER delay\r\n$", 0 );
  48.         abort();
  49.         }
  50.  
  51.         /* The timer interrupt occurs 18.2065 times/sec.
  52.            The form below avoids using floating math.     */
  53.  
  54.     delay = (delay*182)/10;            /* Calculate # of ticks */
  55.     
  56.         /* Install our tick() handler */
  57.  
  58.     original = _dos_getvect( TIMER );    /* save original for chain */
  59.     _dos_setvect( TIMER, tick );        /* then install tick() */
  60.     
  61.         /* This loop watches the value "ticks" which 
  62.            increments in the background at each timer
  63.            interrupt. Compiling with loop optimization 
  64.            will cause the compiler to screw this up.   */
  65.            
  66.     for( ticks=0 ; ticks<delay ; );        /* Count STOP ticks */
  67.  
  68.         /* Remove our tick() handler by replacing
  69.            the original interrupt vector.         */
  70.  
  71.     _dos_setvect( TIMER, original );    /* Remove INT 8 handler */
  72.     
  73.     exit(0);
  74. }